//
//  BDForwardViewController.m
//  BadBoy
//
//  Created by wen on 2017/4/6.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDForwardViewController.h"
#import "BDTextView.h"
#import "CameraPhotoBehavior.h"
#import "BDHomeCellMerchantsView.h"
#import "LiuqsEmoticonKeyBoard.h"
#import "BDEmojiKeyboardBtton.h"
#import "BDLocationServer.h"

@interface BDForwardViewController ()

@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIButton *fasongButton;
@property (nonatomic, strong) BDTextView *textView;
@property (nonatomic, strong) BDHomeCellMerchantsView *forwardView;
@property (nonatomic, strong) LiuqsEmoticonKeyBoard *emoticonKeyborad;
@end

@implementation BDForwardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = ASLocalizedString(@"转发");
    
    [self.view addSubview:self.contentView];
    self.contentView.top = kBDTopHeight + 10;
    
    [self.view addSubview:self.fasongButton];
    self.emoticonKeyborad.top = kBDScreenHeight;
    [self binRAC];
}

- (void)binRAC{
    [[self.fasongButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSString *platString = [self.textView.attributedText getPlainString];
        if (platString.length == 0) {
            [MBManager showBriefAlert:ASLocalizedString(@"帖子内容不能为空")clickHidden:NO clickBlock:nil];
            return ;
        }
        // BDLog(ASLocalizedString(@"点击了转发按钮"));
        NSString *url = [NSString stringWithFormat:@"%@post", KBDBaseURL];
        NSDictionary *para = @{@"post":platString,
                               @"price":@(0),
                               @"city_id":[BDLocationServer getCurrentCity_id],
                               @"repost_id":@(self.homeModel.my_id)};
        NSString *headValid = [BDUserModel getHeaderValidation];
        [MBManager showLoadingAlert:ASLocalizedString(@"请稍后")clickHidden:NO clickBlock:nil];
        RACSignal *signal = [[BDNetworkingServer yy_POST:url parameters:para validation:headValid automaticProcessingError:YES] execute:nil];
        [signal subscribeNext:^(id  _Nullable x) {
//            [MBManager hideAlert];
            [MBManager showBriefAlert:ASLocalizedString(@"转发成功")clickHidden:NO clickBlock:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }];
        
        [signal subscribeError:^(NSError * _Nullable error) {
//            [MBManager hideAlert];
            [MBManager showBriefAlert:ASLocalizedString(@"转发失败")clickHidden:NO clickBlock:nil];
        }];
    }];
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        if (self.emoticonKeyborad.bottom == kBDScreenHeight) {
            [UIView animateWithDuration:0.3 animations:^{
                self.emoticonKeyborad.top = kBDScreenHeight;
            } completion:^(BOOL finished) {
                self.emoticonKeyborad.hidden = YES;
            }];
        }
    }];
}

- (void)reloadTableHeaderView{
    CGFloat imageSide = (kBDScreenWidth - 40) / 3;
    
    for (int i = 0; i < self.imageArray.count; i++) {
        UIImageView *imageView = self.imageArray[i];
        imageView.frame = CGRectMake(10 + i % 3 * (imageSide + 10), 125 + i / 3 * (imageSide + 10), imageSide, imageSide);
    }
    
    self.contentView.height = 45 + 120 + 10 + self.imageArray.count % 3 * (imageSide + 10);
    
    NSArray *buttonImageArray = @[@"pic_1",@"biaoqing"];
    for (int i = 0; i < buttonImageArray.count; i++) {
        UIButton *button = [self.contentView viewWithTag:1000 + i];
        button.bottom = self.contentView.height - 10;
    }
}

- (UIView *)contentView{
    if (!_contentView) {
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 227)];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.borderColor = kBDColorGray.CGColor;
        _contentView.layer.borderWidth = 0.5;
        
        self.textView = [[BDTextView alloc]initWithFrame:CGRectMake(10, 0, kBDScreenWidth - 20, 120)];
        [_textView setPlaceHolderFont:[UIFont systemFontOfSize:15]];
        [_textView setPlaceholder:ASLocalizedString(@"发表内容请遵守中华人民共和国法律法规，禁止发表不健康及政治等违规内容。")];
        [_contentView addSubview:_textView];
//        _textView.backgroundColor = [UIColor redColor];
        
        [_contentView addSubview:self.forwardView];
        self.forwardView.top = self.textView.bottom + 5;
//        self.forwardView.backgroundColor = [UIColor greenColor];
        
        NSArray *imageArray = @[@"biaoqing"];//,@"jing"
        CGFloat space = 40;
        
        CGFloat finiBott = 0;
        for (int i = 0; i < imageArray.count; i++) {
            UIButton *button = [[UIButton alloc]init];
            button.size = CGSizeMake(25, 25);
            button.left = 10 + (25 + space) * i;
            button.top = self.forwardView.bottom + 10;
            [button setBackgroundImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
            button.tag = 1000 + i;
            
            [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                // BDLog(ASLocalizedString(@"点击了表情按钮啊"));
                [self.textView resignFirstResponder];
                self.emoticonKeyborad.hidden = NO;
                if (self.emoticonKeyborad.bottom != kBDScreenHeight) {
                    self.emoticonKeyborad.top = kBDScreenHeight;
                    [UIView animateWithDuration:0.3 animations:^{
                        self.emoticonKeyborad.bottom = kBDScreenHeight;
                    }];
                }
            }];
            
            [_contentView addSubview:button];
            finiBott = button.bottom;
        }
        _contentView.height = finiBott + 10;
    }
    return _contentView;
}

- (BDHomeCellMerchantsView *)forwardView{
    if (!_forwardView) {
        self.forwardView = [[BDHomeCellMerchantsView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 80)];
        [_forwardView configeWithModel:self.homeModel forForward:YES];
    }
    return _forwardView;
}

- (UIButton *)fasongButton{
    if (!_fasongButton) {
        self.fasongButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _fasongButton.frame = CGRectMake(10, self.contentView.bottom + 15, self.view.bounds.size.width - 20, 45);
        [_fasongButton setTitle:ASLocalizedString(@"发送")forState:UIControlStateNormal];
        [_fasongButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _fasongButton.backgroundColor = kLoginButtonColor;
        _fasongButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _fasongButton.layer.masksToBounds = YES;
        _fasongButton.layer.cornerRadius = 4;
    }
    return _fasongButton;
}

- (NSMutableArray *)imageArray{
    if (!_imageArray) {
        self.imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (LiuqsEmoticonKeyBoard *)emoticonKeyborad{
    if (!_emoticonKeyborad) {
        self.emoticonKeyborad = [LiuqsEmoticonKeyBoard showKeyBoardInView:self.view];
        _emoticonKeyborad.textView = self.textView;
        [self.view addSubview:_emoticonKeyborad];
    }
    return _emoticonKeyborad;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillShowNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
